package main

import (
	"fmt"
	"math/rand"
	"sort"
	"time"
)

type Hero struct {
	Name string
	Age  int
}

//声明一个结构体切片类型
type HeroSlice []Hero

func (hs HeroSlice) Len() int {
	return len(hs)
}

func (hs HeroSlice) Less(i, j int) bool {
	return hs[i].Age > hs[j].Age
}

func (hs HeroSlice) Swap(i, j int) {
	/*
		tmp := hs[i]
		hs[i] = hs[j]
		hs[j] = tmp
	*/
	hs[i], hs[j] = hs[j], hs[i]
}
func main() {
	var heroes HeroSlice
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < 10; i++ {
		hero := Hero{
			Name: fmt.Sprintf("英雄%d", i),
			Age:  rand.Intn(101),
		}
		heroes = append(heroes, hero)
	}
	for _, v := range heroes {
		fmt.Println(v)
	}
	fmt.Println("------------------------------")
	sort.Sort(heroes)
	for _, v := range heroes {
		fmt.Println(v)
	}
}
